package com.tsmti.lab.controller.system;

import com.alibaba.fastjson.JSONArray;
import com.tsmti.core.common.controller.BaseCrudController;
import com.tsmti.core.common.model.AjaxJson;
import com.tsmti.core.util.ConvertUtil;
import com.tsmti.shiro.annotation.RequiresPathPermission;
import com.tsmti.system.entity.Menu;
import com.tsmti.system.entity.Role;
import com.tsmti.system.entity.RoleMenu;
import com.tsmti.system.service.MenuService;
import com.tsmti.system.service.RoleMenuService;
import com.tsmti.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Package：com.tsmti.lab.controller.system
 * @Class：RoleController
 * @Description： 角色
 * @Author：zcw
 * @Date：Created in 2018/3/14 17:15
 * @Company:
 * @Version：
 * @Modified By:
 */
@Controller
@RequestMapping("/system/role")
@RequiresPathPermission("sys:role")
public class RoleController  extends BaseCrudController<Role,Long> {
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleMenuService roleMenuService ;

    @RequestMapping(value = "authMenu", method = RequestMethod.GET)
    public String authMenu(Role role, Model model, HttpServletRequest request, HttpServletResponse response) {
        List<Menu> menus = menuService.list();
        List<Menu> selectMenus = menuService.findMenuByRoleId(role.getId());
        model.addAttribute("menus", JSONArray.toJSON(menus));
        model.addAttribute("selectMenus", JSONArray.toJSON(selectMenus));
        model.addAttribute("data", role);
        return display("authMenu");
    }

    @RequestMapping(value = "/doAuthMenu", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson doAuthMenu(Role role, HttpServletRequest request, HttpServletResponse response) {
        AjaxJson ajaxJson = AjaxJson.success("权限设置成功");
        try {
            Long roleId = role.getId();
            // 删除菜单关联
            roleMenuService.removeAllByProperty("roleId", roleId);
            String selectMenu = request.getParameter("selectMenus");
            String[] selectMenus = selectMenu.split(",");
            List<RoleMenu> roleMenuList = new ArrayList<RoleMenu>();
            for (String menuId : selectMenus) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(ConvertUtil.getLongValue(menuId));
                roleMenuList.add(roleMenu);
            }
            roleMenuService.saveAll(roleMenuList);
            UserUtils.clearCache();
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson = AjaxJson.fail("权限设置失败");
        }
        return ajaxJson;
    }

}
